package com.apps.ftapp.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.SucModelAdapter;
import com.apps.ftapp.db.Singleton;
import com.apps.ftapp.model.ZhenTiResult;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Timer;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;

public class MyStSucActivity extends BaseActivity {


    @BindView(R.id.gridView)
    GridView gridView;
    @BindView(R.id.tijiao)
    TextView tijiao;

    private Gson gson = new Gson();
    private SucModelAdapter sucModelAdapter;
    private List<Map> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_st_suc);
        ButterKnife.bind(this);


        commTitle.setVisibility(View.GONE);

        daojishi.setVisibility(View.VISIBLE);
        relativeLayout.setVisibility(View.VISIBLE);


//        fenxiang.setImageResource(R.drawable.f_a_share);


        Timer timer = new Timer(daojishi);
        timer.start(daojishi);


        tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                tijiao();


            }
        });

        initView();

    }

    private void initView() {

        Singleton s = Singleton.getInstance();
//        for(int i=0;i<6;i++){
//            Map m = new HashMap();
//            m.put("index",i+1);
//            if(i==2||i==5)
//                m.put("is",false);
//            else
//                m.put("is",true);
//            s.addD(m);
////            dataList.add(m);
//        }
        dataList = s.getDatiList();


        sucModelAdapter = new SucModelAdapter(getApplicationContext(), dataList, "");
        gridView.setAdapter(sucModelAdapter);

    }


    public void tijiao() {

        Singleton s = Singleton.getInstance();
        s.setEndTime(Tools.getTime1());
        L.i("main", ">>>>>>" + gson.toJson(s.getDatas()));
        Ion.with(this)
                .load("http://api.futuyj.com/test/finish")
                .setBodyParameter("userId", Constant.uid)
                .setBodyParameter("token", Constant.token)
                .setBodyParameter("id", s.getId())
                .setBodyParameter("beginTime", s.getStartTime())
                .setBodyParameter("endTime", Tools.getTime())
                .setBodyParameter("Answers", gson.toJson(s.getDatas()))
                .as(new TypeToken<ZhenTiResult>() {
                })
                .setCallback(new FutureCallback<ZhenTiResult>() {
                    @Override
                    public void onCompleted(Exception e, ZhenTiResult result) {
                        L.i("main", ";;;;;+" + result.getSuccess());
                        if (1 == Tools.isIntNull(result.getSuccess())) {
                            HashMap map = new HashMap();

//                            L.i("main",".....");
                            map.put("id", result.getResults().getId());

                            ActivitySkipUtil.skipAnotherActivity(MyStSucActivity.this, MyStSucResultActivity.class, map);
                        } else {

                            Toasty.normal(getApplicationContext(), result.getError()).show();
                        }
                    }
                });

//
//        Singleton s = Singleton.getInstance();
//        L.i("main",s.getStartTime());
//        L.i("main", Tools.getTime());
//        L.i("main",s.getId());
//        RequestParams params = new RequestParams();
//        params.put("token", Constant.token);
//        params.put("userId",Constant.uid);
//        params.put("id",s.getId());
//        params.put("beginTime",s.getStartTime());
//        params.put("endTime",Tools.getTime());
//        params.put("Answers",gson.toJson(s.getDatas()));
//        L.i("main",">>>>>>"+gson.toJson(s.getDatas()));
//        params.put("usedTime",10);
//        Async.post("test/finish",params, new AsyncHttpResponseHandler() {
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//                if(statusCode==200){
//                    L.i("main",new String(responseBody));
//
//                    ZhenTiResult result = gson.fromJson(new String(responseBody),ZhenTiResult.class);
//                    if(1== Tools.isIntNull(result.getSuccess())) {
//                        HashMap map = new HashMap();
//                        map.put("id",result.getResults().getId());
//
//                        ActivitySkipUtil.skipAnotherActivity(MyStSucActivity.this,MyStSucResultActivity.class,map);
//                    }
//
//
//                    // ActivitySkipUtil.skipAnotherActivity(GestureFilpActivity.this,SubjectResultActivity.class,true);
//
////                    SubjectResultModel result = gson.fromJson(new String(responseBody),SubjectResultModel.class);
////                    if(1==Tools.isIntNull(result.getSuccess())){
////
////                        ActivitySkipUtil.skipAnotherActivity(MyStActivity.this,SubjectResultActivity.class,true);
////
////                    }else{
////
////                        Toasty.normal(getApplicationContext(),result.getError()).show();
////                    }
//                }
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//
//            }
//        });
    }

}
